<template>
  <t-popup expand-animation placement="bottom-right" trigger="click" v-model:visible="isVisible">
    <template #content>
      <div class="header-msg">
        <div class="header-msg-top">
          <p>通知</p>
          <t-button
            v-if="store.msgData.length > 0"
            class="clear-btn"
            variant="text"
            theme="primary"
            @click="setRead('all')"
            >清空</t-button
          >
        </div>
        <t-list v-if="store.notifNumber > 0" class="narrow-scrollbar" :split="true">
          <t-list-item v-for="(item, index) in store.msgData" :key="index">
            <div @click="goDetail">
              <p class="msg-content">{{ item.title }}</p>
            </div>
            <p class="msg-time" @click="goDetail">{{ item.createDate }}</p>
            <template #action>
              <t-button size="small" variant="outline" @click="setRead('radio', item)"> 设为已读 </t-button>
            </template>
          </t-list-item>
        </t-list>

        <div v-else class="empty-list">
          <img src="https://tdesign.gtimg.com/pro-template/personal/nothing.png" alt="空" />
          <p>暂无通知</p>
        </div>
        <div class="header-msg-bottom">
          <t-button class="header-msg-bottom-link" variant="text" theme="primary" @click="goDetail">查看全部</t-button>
        </div>
      </div>
    </template>
    <t-badge :count="store.notifNumber" :offset="[22, 8]">
      <t-button theme="default" shape="square" variant="text" style="margin: 0 24px">
        <t-icon name="notification" />
      </t-button>
    </t-badge>
  </t-popup>
</template>

<script setup lang="ts">
// @ts-nocheck
import { useSettingStore, useUserStore } from '@/store';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useNotificationStore } from '@/store';
import type { NotificationItem } from '@/types/interface';
import { ref } from 'vue';
const isVisible = ref(false);
const userStore = useUserStore();
const router = useRouter();
const store = useNotificationStore();
// const { msgData, unreadMsg } = storeToRefs(store);

if (userStore.userInfo.info) {
  store.getList();
}
const setRead = (type: string, item?: any) => {
  if (type === 'all') {
    store.splitAll();
  } else {
    store.splitArr(item.id);
  }
};

const goDetail = () => {
  router.push('/news/myNews');
  isVisible.value = false;
};
</script>

<style lang="less" scoped>
.header-msg {
  width: 400px;
  height: 500px;

  .empty-list {
    height: calc(100% - 104px);
    text-align: center;
    padding-top: 135px;
    font-size: 16px;
    color: var(--td-text-color-secondary);

    img {
      width: 63px;
    }

    p {
      margin-top: 30px;
    }
  }

  &-top {
    position: relative;
    height: 56px;
    font-size: 18px;
    color: var(--td-text-color-primary);
    text-align: center;
    line-height: 56px;
    border-bottom: 1px solid var(--td-component-border);

    .clear-btn {
      position: absolute;
      top: 12px;
      right: 24px;
    }
  }

  &-bottom {
    height: 48px;
    align-items: center;
    display: flex;
    justify-content: center;

    &-link {
      text-decoration: none;
      font-size: 16px;
      color: var(--td-brand-color);
      line-height: 48px;
      cursor: pointer;
    }
  }

  .t-list {
    height: calc(100% - 104px);
  }

  .t-list-item {
    overflow: hidden;
    width: 100%;
    padding: 16px 24px;
    border-radius: var(--td-radius-default);
    font-size: 16px;
    color: var(--td-text-color-primary);
    line-height: 22px;
    cursor: pointer;

    &:hover {
      transition: background 0.2s ease;
      background: var(--td-bg-color-container-hover);

      .msg-content {
        color: var(--td-brand-color);
      }

      .t-list-item__action {
        button {
          bottom: 16px;
          opacity: 1;
        }
      }

      .msg-time {
        bottom: -6px;
        opacity: 0;
      }
    }

    .msg-content {
      margin-bottom: 16px;
    }

    .msg-type {
      color: var(--td-text-color-secondary);
    }

    .t-list-item__action {
      button {
        opacity: 0;
        position: absolute;
        right: 24px;
        bottom: -6px;
      }
    }

    .msg-time {
      transition: all 0.2s ease;
      opacity: 1;
      position: absolute;
      right: 24px;
      bottom: 16px;
      color: var(--td-text-color-secondary);
    }
  }
}

.t-button {
  margin: 0 8px;

  .t-icon {
    font-size: 24px;
    &.general {
      margin-right: 16px;
    }
  }
}
</style>
